using System;
using System.Data;

namespace PDO.DataPersistence.Core
{

    public static class SqlConvert
    {

        public static System.Int32? IntNullable(object val)
        {
            if (val == null || val == DBNull.Value)
            {
                return null;
            }
            else
            {
                return IntSafe(val);
            }
        }

        public static int IntSafe(object val)
        {
            if (val is System.Int32)
            {
                return (int)val;
            }
            else
            {
                return 0;
            }
        }

        public static System.Int16 Int16Safe(object val)
        {
            if (val is System.Int16)
            {
                return (System.Int16)val;
            }
            else
            {
                return 0;
            }
        }

        public static System.Int16? Int16Nullable(object val)
        {
            if (val == null || val == DBNull.Value)
            {
                return null;
            }
            else
            {
                return Int16Safe(val);
            }
        }



        public static System.Int64 Int64Safe(object val)
        {
            if (val is System.Int64)
            {
                return (System.Int64)val;
            }
            else
            {
                return 0;
            }
        }

        public static System.Int64? Int64Nullable(object val)
        {
            if (val == null || val == DBNull.Value)
            {
                return null;
            }
            else
            {
                return Int64Safe(val);
            }
        }

        public static System.Boolean BoolSafe(object val)
        {
            if (val is bool)
            {
                return (bool)val;
            }
            else
            {
                return false;
            }
        }

        public static byte ByteSafe(object val)
        {
            if (val == null || val == DBNull.Value)
                return 0;

            if (val is byte)
            {
                return (byte)val;
            }
            else
            {
                return 0;
            }
        }

        public static byte[] ImageSafe(object val)
        {
            if (val == null || val == DBNull.Value)
                return null;

            if (val is byte[])
            {
                return (byte[])val;
            }
            else
            {
                return null;
            }
        }

        public static System.DateTime DTNoValue
        {
            get
            {
                return new DateTime(1, 1, 1);
            }
        }


        public static System.DateTime DateTimeSafe(object val)
        {
            if (val is DateTime)
            {
                return (DateTime)val;
            }
            else
            {
                return  DateTime.MinValue;
            }
        }

        /// <summary>
        /// Return null or a DateTime as determined by argument.  No exception trapping.
        /// </summary>
        public static System.DateTime? DateTimeNullable(object val)
        {
            if (val == null || val == DBNull.Value )
            {
                return null;
            }
            else
            {
                return DateTimeSafe(val);
            }
        }

        public static System.Decimal DecimalSafe(object val)
        {
            try
            {
                return Convert.ToDecimal(val, System.Globalization.CultureInfo.InvariantCulture);
            }
            catch (System.InvalidCastException)
            {
                return 0;
            }
        }

        /// <summary>
        /// Return null or a Decimal determined by argument.  No exception trapping.
        /// </summary>
        public static System.Decimal? DecimalNullable(object val)
        {
            if (val == null || val == DBNull.Value || val.ToString().Length == 0)
            {
                return null;
            }
            else
            {
                return Convert.ToDecimal(val, System.Globalization.CultureInfo.InvariantCulture);
            }
        }

        public static double DoubleSafe(object val)
        {
            if (val is double)
            {
                return (double)val;
            }
            else
            {
                return 0;
            }
        }



        public static float FloatSafe(object val)
        {
            if (val is float)
            {
                return (float)val;
            }
            else
            {
                return 0;
            }
        }


        public static Guid GuidSafe(object val)
        {
            if (val == null || val == DBNull.Value)
            {
                return Guid.Empty;
            }
            return (Guid)val;
        }

        public static Guid? GuidNullable(object val)
        {
            if (val == null || val == DBNull.Value)
            {
                return null;
            }
            return (Guid)val;
        }

        public static object VariantSafe(object val)
        {
            if (val == null || val == DBNull.Value)
            {
                return null;
            }
            return val;
        }


        public static string StringSafe(object val)
        {
            if(val is string)
            {
                return val.ToString();
            }
            else
            {
                return string.Empty;
            }
        }


        public static string DateString(object date)
        {
            string s;
            try
            {
                s = ((DateTime)date).ToShortDateString();
            }
            catch (System.InvalidCastException)
            {
                s = string.Empty;
            }
            return s;
        }


        public static string AmountString(object amount)
        {
            string s;
            if (amount is double)
            {
                s = ((double)amount).ToString("f2", System.Globalization.CultureInfo.InvariantCulture);
            }
            else
            {
                s = string.Empty;
            }

            return s;
        }


    }

}
